package com.Controller;


import com.Service.impl.StudentServiceImpl;
import com.domain.Student;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*
@WebServlet("/studentServlet")
public class studentServlet extends HttpServlet {
    private StudentServiceImpl service = new StudentServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);//设置请求和响应的编码
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //获取请求方法名
        String method = request.getParameter("method");
        if (method.equals("selectByPage")){
            //分页查询
            selectByPage(request,response);
        }else if (method.equals("addStudent")){
            addStudent(request,response);
        }else if (method.equals("delStudent")){
            delStudent(request,response);
        }else if (method.equals("updateStudent")){
            updateStudent(request,response);
        }else if (method.equals("findStu")){
            findStu(request,response);
        }
    }

    */
/**
     * 高级查询
     * @param request
     * @param response
     *//*

    private void findStu(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        Student student = new Student();
        if (name.equals("undefined")){
        }else {
            student.setName(name);
        }
        if (sex.equals("undefined")){
        }else {
            student.setSex(sex);
        }

        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");
        //调用业务层
        Page page = service.findStu(student, Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        PageInfo<List<Student>> info = new PageInfo<>(page);
        //将集合对象转化为json
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(info);
        //将json对象发送至客户端
        response.getWriter().write(json);
    }

    */
/**
     * 修改学生
     * @param request
     * @param response
     *//*

    private void updateStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数
        Map<String, String[]> pm = request.getParameterMap();
        //创建对象
        Student student = new Student();
        //封装数据
        try {
            BeanUtils.populate(student, pm);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        String id = request.getParameter("id");
        student.setId(Integer.parseInt(id));
        //调用业务层
        Integer count = service.updateStu(student);
        if (count ==1){
            response.getWriter().write("true");
        }
    }

    */
/**
     * 删除学生
     * @param request
     * @param response
     * @throws IOException
     *//*

    private void delStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数
        String id = request.getParameter("id");
        //调用业务层
        Integer count = service.delStu(Integer.parseInt(id));
        if (count ==1){
            response.getWriter().write("true");
        }
    }

    */
/**
     * 添加学生
     * @param request
     * @param response
     * @throws IOException
     *//*

    private void addStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数
        Map<String, String[]> pm = request.getParameterMap();
        //创建对象
        Student student = new Student();
        //封装数据
        try {
            BeanUtils.populate(student, pm);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //调用业务层
        Integer count = service.addStu(student);
        if (count ==1){
           response.getWriter().write("true");
        }
    }

    */
/**
     * 分页查询学生
     * @param request
     * @param response
     * @throws IOException
     *//*

    private void selectByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");
        //得到数据库用户数据,并返回
        Page page = service.pageQuery(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        PageInfo<List<Student>> info = new PageInfo<>(page);
        //将集合对象转化为json
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(info);
        //将json对象发送至客户端
        response.getWriter().write(json);
    }
    */
/*
    日期转换
 *//*

    private void dateConvert() {
        ConvertUtils.register(new Converter() {
            public Object convert(Class type, Object value) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    return simpleDateFormat.parse(value.toString());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }, Date.class);
    }
}
*/
